package co.yixiang.modules.system.util;

import org.apache.commons.lang.StringUtils;


public class InfoFilterUtil {
    //匹配用户名
    public static String nameDesensitization(String name) {
        if (name == null || name.isEmpty()) {
            return "";
        }
        String myName = null;
        char[] chars = name.toCharArray();
        if (chars.length == 1) {
            myName = name;
        }
        if (chars.length == 2) {
            myName = name.replaceFirst(name.substring(1), "*");
        }
        if (chars.length > 2) {
            myName = name.replaceAll(name.substring(1, chars.length - 1), "*");
        }
        return myName;
    }

    // 手机号码前三后四脱敏
    public static String mobileEncrypt(String phone) {
        if (StringUtils.isEmpty(phone) || (phone.length() != 11)) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }


    /**
     * 对客户姓名做掩码
     *
     * @throws
     */
    public static String maskUserName(String userName) throws Exception {
        if (userName == null || userName.length() == 0) return "";
        String v = userName.substring(0, 1);
        return StringUtils.rightPad(v, userName.length(), "*");//StringUtils.rightPad方法做一个字符串右补齐
    }


}

